let imgObj = {
  src: "",
  width: 0,
  height: 0
}

function ImgInfo(node){
    this.src = node.src
    this.width = node.naturalWidth
    this.height = node.naturalHeight
}

// 获取所有父级dom元素
function getParentTag(startTag, parentTagList = []) {
  // 传入标签是否是DOM对象
  if (!(startTag instanceof HTMLElement)) return console.error('receive only HTMLElement');
  // 父级标签是否是body,是着停止返回集合,反之继续
  if ('BODY' !== startTag.parentElement.nodeName) {
      if(startTag.tagName === 'A') {
          return parentTagList
      }
    // 放入集合
    parentTagList.push(startTag.parentElement)
    // 再上一层寻找
    return getParentTag(startTag.parentElement, parentTagList)
  }
  // 返回集合,结束
  else return parentTagList;

}
// 判断父级元素是否为链接
function isLink(parents) {
  for(let i = 0; i < parents.length; i++) {
    if(!!parents[i].href){
        return [true, parents[i]]
    }
  }
  return [false, null]
}
// 点击后将当前图片信息传入IOS
function clickEvent() {
   let imgInfo = new ImgInfo(this)
   //window.webkit.messageHandlers.submitAnImageInfo.postMessage(JSON.stringify(imgInfo))
}


function LongPress(img) {
    let imgInfo = new ImgInfo(img)
    //window.webkit.messageHandlers.submitAnImageInfo.postMessage(JSON.stringify(imgInfo))
}

function wrapByA(imgNode) {
    let parentNode = imgNode.parentNode
    let newA = document.createElement("a")
    parentNode.insertBefore(newA,imgNode)
    newA.appendChild(imgNode)
    if(imgNode.src.indexOf("?") !== -1) {
        newA.href = imgNode.src + "&isImage=true"
    }else{
        newA.href = imgNode.src + "?isImage=true"
    }
    
}

function hasOtherChild(parents) {
    for(let j = 0; j < parents.length; j++) {
        if(parents[j].children.length > 1) {
            return true
        }
    }
    return false
}

let objs_filtered = []
// 为所有不为链接的图片加上点击事件
function AddImgClickEvent() {
  //window.webkit.messageHandlers.testPostMessage.postMessage("inside AddImgClickEvent")
  let objs = document.getElementsByTagName("img");
  let timer = null
  
  for (let i = 0; i < objs.length; i++) {
      if(objs[i].src != "" && !(objs[i].naturalWidth < 5 || objs[i].naturalHeight < 5)){
          //objs[i].addEventListener('click', clickEvent);
          objs_filtered.push(new ImgInfo(objs[i]));
          //wrapByA(objs[i])
          let parents = getParentTag(objs[i])
          let islink = isLink(parents)
          if (islink[0]) {
              if(!hasOtherChild(parents)) {
//                  let url = islink[1].href
//                  if(url.indexOf("?") !== -1) {
//                      islink[1].href += "&isImage=true"
//                  }else{
//                      islink[1].href += "?isImage=true"
//                  }
                  console.log("!hasotherchild",objs[i].src)
                  console.log(parents)
                  wrapByA(objs[i])
              }
          }else {
              console.log("not link",objs[i].src)
              wrapByA(objs[i])
          }
          
//          objs[i].addEventListener("touchstart", function (e) {
//               console.log('touchstart');
//               timer = setTimeout(function () {
//                   console.log('LongPress');
//                   e.preventDefault();
//                   //LongPress(objs[i]);
//               }, 800);
//           });
//          objs[i].addEventListener("touchmove", function (e) {
//               console.log('touchmove');
//               clearTimeout(timer);
//               timer = 0;
//           });
//          objs[i].addEventListener("touchend", function (e) {
//               console.log('touchend');
//               clearTimeout(timer);
//               //if (timer != 0) {
//               //    alert('这是点击，不是长按');
//               //}
//               return false;
//           });
      }

  }
  

  //window.webkit.messageHandlers.setImageAsset.postMessage(JSON.stringify(objs_filtered));
    

}
AddImgClickEvent();

var startX, startY;
document.addEventListener('touchstart',function (ev) {
    startX = ev.touches[0].pageX;
    startY = ev.touches[0].pageY;
}, false);

document.addEventListener('touchend',function (ev) {
    var endX, endY;
    endX = ev.changedTouches[0].pageX;
    endY = ev.changedTouches[0].pageY;
    if(startY - endY > 10) {
        AddImgClickEvent();
        console.log("下滑刷新")
    }
    
}, false);


